package com.tanhua.server.controller;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.domain.Answers;
import com.tanhua.model.vo.ConclusionVo;
import com.tanhua.model.vo.QuestionnaireVo;
import com.tanhua.server.service.TestSoulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/testSoul")
public class TestSoulController {
    @Autowired
    private TestSoulService testSoulService;

    //查询问卷
    @GetMapping
    public ResponseEntity findQuestionnaire(){
        List<QuestionnaireVo> vos = testSoulService.findQuestionnaire();
        return ResponseEntity.ok(vos);

    }

    //提交问卷
    @PostMapping
    public ResponseEntity commitResult(@RequestBody Map map){
        List<Answers> answers = (List<Answers>) map.get("answers");
        List<String> optionId = CollUtil.getFieldValues(answers, "optionId", String.class);
        List<String> questionId = CollUtil.getFieldValues(answers, "questionId", String.class);
        String result = testSoulService.commitResult(optionId, questionId);
        return ResponseEntity.ok(result);


    }
    //查看结果
    @GetMapping("/report/{id}")
    public ResponseEntity report(@PathVariable("id") String reportId){
        ConclusionVo vo =testSoulService.report(reportId);

        return ResponseEntity.ok(vo);
    }


}
